package net.blank.ui
{
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import net.blank.bitmapOperation.BitmapDraw;
	import net.blank.functionUtl.FunctionBinder;
	import net.blank.ui.core.InteractiveUIEvent;
	import net.blank.ui.core.UIComponent;
	
	/**
	 * 按钮
	 * @author blank
	 * 
	 */
	public class Button extends UIComponent
	{
		/**
		 * 状态-普通
		 */
		public static const STATUS_NORMAL:uint=0;
		
		/**
		 * 状态-鼠标位于上方
		 */
		public static const STATUS_OVER:uint=1;
		
		/**
		 * 状态-鼠标按下
		 */
		public static const STATUS_DOWN:uint=2;
		
		/**
		 * 当前的状态
		 */
		private var _status:uint;
		
		/**
		 * 是否锁定
		 */
		private var _isLock:Boolean;
		
		/**
		 * 存储除去 状态为 OVER 或 DOWN 时添加的禁用状态滤镜之外的其它滤镜
		 */
		private var _filters:Array;
		
		/**
		 * 鼠标位于上方
		 * @param evt
		 * 
		 */
		private function mouseOverHandler(evt:MouseEvent):void{
			if(!_isLock){
				_status=STATUS_OVER;
				
				updateBackground();
				updateFilters();
				
				evt.updateAfterEvent();
				
				/*声效*/
			}
		}
		
		/**
		 * 鼠标离开
		 * @param evt
		 * 
		 */
		private function mouseOutHandler(evt:MouseEvent):void{
			if(!_isLock){
				_status=STATUS_NORMAL;
				
				updateBackground();
				updateFilters();
				
				evt.updateAfterEvent();
				
				/*声效*/
			}
		}
		
		/**
		 * 鼠标按下
		 * @param evt
		 * 
		 */
		private function mouseDownHandler(evt:MouseEvent):void{
			if(!_isLock){
				_status=STATUS_DOWN;
				
				updateBackground();
				updateFilters();
				
				evt.updateAfterEvent();
				
				/*声效*/
			}
		}
		
		/**
		 * 鼠标释放
		 * @param evt
		 * 
		 */
		private function mouseUpHandler(evt:MouseEvent):void{
			if(!_isLock){
				_status=STATUS_OVER;
				
				updateBackground();
				updateFilters();
				
				evt.updateAfterEvent();
				
				/*声效*/
			}
		}
		
		/**
		 * 对各个状态的背景进行渲染
		 * @param background
		 * @param clipCahce
		 * @return 
		 * 
		 */
		protected function drawBackground(background:BitmapData,clipCahce:Object):Boolean{
			if(background){
				$drawRect.width=_width;
				$drawRect.height=_height;
				
				if(_skin.getInnerScaleRect()){
					BitmapDraw.drawForClips(graphics,$drawRect,clipCahce);
				}else{
					BitmapDraw.drawBitmap(graphics,background,$drawRect);
				}
				
				return true;
			}
			return false;
		}
		
		/**
		 * 对各个状态的滤镜进行渲染
		 * @param filters
		 * @return 
		 * 
		 */
		protected function renderFilters(filters:Array):void{
			if(filters){
				if(_filters){
					noRenderDelay::filters=_filters.concat(filters);
				}else{
					noRenderDelay::filters=filters;
				}
			}else{
				noRenderDelay::filters=_filters;
			}
		}
		
		override protected function update():void{
			/*设置坐标*/
			if(isNeedUpdateCoordinate){
				noRenderDelay::x=_x;
				noRenderDelay::y=_y;
				
				isNeedUpdateCoordinate=false;
			}
			
			/*绘制背景*/
			if(isNeedUpdateBackground){
				graphics.clear();
				
				if(_width > 0 && _height > 0 && _skin){
					if(enabled){
						switch(_status){
							case STATUS_NORMAL:
								drawBackground(_skin.getBackground(),_skin.clipCache);
								break;
							case STATUS_OVER:
								if(!drawBackground(_skin.getOverBackGround(),_skin.overClipCache)){
									drawBackground(_skin.getBackground(),_skin.clipCache);
								}
								break;
							case STATUS_DOWN:
								if(!drawBackground(_skin.getDownBackGround(),_skin.downClipCache)){
									drawBackground(_skin.getBackground(),_skin.clipCache);
								}
								break;
						}
					}else{
						if(!drawBackground(_skin.getDisabledBackGround(),_skin.disabledClipCache)){
							drawBackground(_skin.getBackground(),_skin.clipCache);
						}
					}
				}
				
				isNeedUpdateBackground=false;
			}
			
			/*设置滤镜*/
			if(isNeedUpateFilters && _skin){
				if(enabled){
					switch(_status){
						case STATUS_NORMAL:
							noRenderDelay::filters=_filters;
							break;
						case STATUS_OVER:
							renderFilters(_skin.getOverFilters());
							break;
						case STATUS_DOWN:
							renderFilters(_skin.getDownFilters());
							break;
					}
				}else{
					renderFilters(_skin.getDisabledFilters());
				}
				
				isNeedUpateFilters=false;
			}
		}
		
		override protected function enabledChangeHandler(evt:InteractiveUIEvent):void{
			updateBackground();
			updateFilters();
		}
		
		/**
		 * 按钮
		 * 
		 */
		public function Button(){
			super();
			
			super.buttonMode=true;
			super.mouseChildren=false;
			
			addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
		}
		
		/**
		 * 皮肤的背景发生改变,更新背景
		 * @param bmp
		 * 
		 */
		override protected function skin_changedBackground():void{
			if(_status == STATUS_NORMAL && enabled){
				updateBackground();
			}
		}
		
		/**
		 * 皮肤的9格缩放区域发生改变,更新背景
		 * @param rect
		 * 
		 */
		override protected function skin_changedInnerScaleRect():void{
			if(_status == STATUS_NORMAL && enabled){
				updateBackground();
			}
		}
		
		/**
		 * 皮肤的"鼠标移上状态的图像"发生改变
		 * 
		 */
		protected function skin_changedOverBackground():void{
			if(_status == STATUS_OVER && enabled){
				updateBackground();
			}
		}
		
		/**
		 * 皮肤的"鼠标移上状态的滤镜"发生改变
		 * 
		 */
		protected function skin_changedOverFilters(value:Array):void{
			if(_status == STATUS_OVER && enabled){
				updateFilters();
			}
		}
		
		/**
		 * 皮肤的"鼠标按下状态的图像"发生改变
		 * 
		 */
		protected function skin_changedDownBackground(value:BitmapData):void{
			if(_status == STATUS_DOWN && enabled){
				updateBackground();
			}
		}
		
		/**
		 * 皮肤的"鼠标按下状态的滤镜"发生改变
		 * 
		 */
		protected function skin_changedDownFilters(value:Array):void{
			if(_status == STATUS_DOWN && enabled){
				updateFilters();
			}
		}
		
		/**
		 * 皮肤的"禁用交互性事件的图像"发生改变
		 * 
		 */
		protected function skin_changedDisabledBackground(value:BitmapData):void{
			if(!enabled){
				updateBackground();
			}
		}
		
		/**
		 * 皮肤的"禁用交互性事件的滤镜"发生改变
		 * @param value
		 * 
		 */
		override protected function skin_changedDisabledFilters():void{
			if(!enabled){
				updateFilters();
			}
		}
		
		override protected function bindingSkin():void{
			super.bindingSkin();
			
			FunctionBinder.addBindingFun(_skin.setOverBackGround,skin_changedOverBackground);
			FunctionBinder.addBindingFun(_skin.setOverFilters,skin_changedOverFilters);
			FunctionBinder.addBindingFun(_skin.setDownBackGround,skin_changedDownBackground);
			FunctionBinder.addBindingFun(_skin.setDownFilters,skin_changedDownFilters);
			FunctionBinder.addBindingFun(_skin.setDisabledBackGround,skin_changedDisabledBackground);
		}
		
		override protected function unbindingSkin():void{
			super.unbindingSkin();
			
			FunctionBinder.rmBindingFun(_skin.setOverBackGround,skin_changedOverBackground);
			FunctionBinder.rmBindingFun(_skin.setOverFilters,skin_changedOverFilters);
			FunctionBinder.rmBindingFun(_skin.setDownBackGround,skin_changedDownBackground);
			FunctionBinder.rmBindingFun(_skin.setDownFilters,skin_changedDownFilters);
			FunctionBinder.rmBindingFun(_skin.setDisabledBackGround,skin_changedDisabledBackground);
		}
		
		override public function set filters(value:Array):void{
			if(value != _filters){
				_filters=value;
				
				updateFilters();
			}
		}
		override public function get filters():Array{
			return _filters;
		}
		
		override public function destroy():void{
			_filters=null;
			
			super.destroy();
		}
		
		override public function reset():void{
			_status=STATUS_NORMAL;
			
			filters=null;
			
			super.reset();
		}
	}
}